package com.study.suanfa.shu.erchashu;

/**
 * @description: BinaryTreeTest
 * @date: 2020/12/3 19:45
 * @author: hcm
 * @version: 1.0
 */
public class BinaryTreeTest {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "齐天大圣");
        HeroNode nodeLeft1 = new HeroNode(2, "牛魔王");
        HeroNode nodeLeft2 = new HeroNode(3, "红孩儿");
        HeroNode nodeRight1 = new HeroNode(4, "老夫子");
        HeroNode nodeRight2 = new HeroNode(5, "走地鸡");
        HeroNode nodeRight3 = new HeroNode(6, "太白星君");

        root.setLeft(nodeLeft1);
        nodeLeft1.setLeft(nodeLeft2);
        root.setRight(nodeRight1);
        nodeRight1.setRight(nodeRight2);
        nodeRight2.setRight(nodeRight3);
        binaryTree.setRoot(root);
        binaryTree.preOrder();
        binaryTree.preOrderSeach(5);
        binaryTree.delNode(6);
        binaryTree.preOrder();
    }
}

/**
 * 构建二叉树
 */
class BinaryTree {
    private HeroNode root;

    public HeroNode getRoot() {
        return root;
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public BinaryTree() {

    }

    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        }
    }

    public void preOrderSeach(int no) {
        if (this.root != null) {
            System.out.println("查找第" + no + "个节点为： " + this.root.preOrderSerch(no));
        }
    }

    /**
     * 递归删除节点
     */
    public void delNode(int no) {
        if (root == null) {
            System.out.println("空树无法删除");
            return;
        }
        if (this.root.getNo() == no) {
            root = null;
        } else {
            root.delNode(no);
        }
    }
}

class HeroNode {

    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 前序遍历方法
     */
    public void preOrder() {
        //输出当前节点
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }

    }

    /**
     * 后序
     */
    public void postOrder() {
        if (this.left != null) {
            this.left.midOrder();
        }

        if (this.right != null) {
            this.right.midOrder();
        }
        System.out.println(this);
    }

    /**
     * 前序遍历查找
     */
    public HeroNode preOrderSerch(int no) {
        //比较当前节点
        if (this.no == no) {
            return this;
        }
        HeroNode res = null;
        if (this.left != null) {
            res = this.left.preOrderSerch(no);
        }
        if (res != null) {
            return res;
        }
        if (this.right != null) {
            res = this.right.preOrderSerch(no);
        }
        if (res != null) {
            return res;
        }
        return res;
    }

    public void delNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }
}
